package com.xw.audioplayer;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xw.floatlib.FloatActionController;
import com.xw.floatlib.MusicData;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<MusicData> musicData = new ArrayList<>();
    private int mProgress = 0;

    public MyAdapter(List<MusicData> musicData) {
        this.musicData = musicData;
//        FloatMonkService.registerCallback(this);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_items,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
//            viewHolder.data = musicData.get(i);
        if (!musicData.get(i).isPlaying()) {
            viewHolder.title.setText(musicData.get(i).getTitle());
        }else{
            viewHolder.title.setText("当前播放进度:" + musicData.get(i).getProgress());
        }
    }

    @Override
    public int getItemCount() {
        return musicData.size();
    }
//
//    @Override
//    public void updateProgress(int progress, int maxLength) {
//        mProgress = progress;
//        notifyDataSetChanged();
//    }

//    @Override
//    public void setItems(MusicData items) {
//
//    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        MusicData data;

        public ViewHolder(@NonNull final View itemView) {
            super(itemView);

            title = itemView.findViewById(R.id.title);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    for (MusicData data:musicData) {
//
//                        if (data.isPlaying()){
//                            data.setPlaying(false);
//                        }
//                    }
//
//                    musicData.get(getAdapterPosition()).setPlaying(true);

//                    FloatMonkService.registerCallback(new MusicCallBack() {
//                        @Override
//                        public void updateProgress(int progress, int maxLength,String musicId) {
////                            mProgress = progress;
////                            notifyDataSetChanged();
//                            if (data.getMusicId().equals(musicId)){
//                                title.setText("当前进度：" + progress);
//                            }
//
//                        }
//
//                        @Override
//                        public void setItems(MusicData items) {
//
//                        }
//                    });

                    if (FloatActionController.getInstance().isStartService)
                        FloatActionController.getInstance().playItem(getAdapterPosition(), musicData,"0");
                    else
                        //开启服务
                        FloatActionController.getInstance().startMonkServer(itemView.getContext(),musicData,getAdapterPosition(),"0");

                }
            });
        }
    }
}
